package com.ruoyi.ftrl.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.QRCodeUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

@RestController
@RequestMapping("/api/qrcode")
public class QrCodeController extends BaseController{
    /**
     * 生成普通二维码
     *
     * @param content 二维码内容
     * @param response HttpServletResponse
     */
    @Anonymous
    @GetMapping("/generate")
    public void generateQRCode(@RequestParam String content, HttpServletResponse response) {
        try {
            // 设置响应类型为图片
            response.setContentType("image/png");

            // 生成二维码
            BufferedImage qrCodeImage = QRCodeUtil.generateQRCode(content, 300, 300);

            // 输出到响应流
            ImageIO.write(qrCodeImage, "png", response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

